package com.jinzw.jform.rows

import android.app.AlertDialog
import android.content.Context
import android.support.v7.widget.AppCompatTextView
import android.view.Gravity
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView

import com.jinzw.jform.R
import com.jinzw.jform.core.DimensionUtils
import com.jinzw.jform.core.DimensionUtils.dpToPx
import android.content.DialogInterface
import android.util.TypedValue
import com.jinzw.jform.core.ResultHandler


open class TextShowLeftRow(context: Context) : AbstractTextRow<String>(context) {

    //will be created by onCreateView
    var dateView: TextView? = null

    var potsion:Int =0

    var value: String? = null
        set(value) {
            field = value
            dateView?.text = value
        }
    var defaultDate: String? = null

    var alertTitle: String = ""
    var alertItem: Array<String>? = null



    /**
     * Change this lambda to have a custom Date Picker (on click)
     */


    protected var customizeDateView: ((TextShowLeftRow, TextView) -> Unit) = { dateRow, textView ->



    }

    /**
     * Change this lambda to have a custom Date View
     */
    fun customizeDateView(block: (TextShowLeftRow, TextView) -> Unit) {
        this.customizeDateView = block
    }

    init {
        validator = { v -> v != null }
        onCreateView<TextShowLeftRow> {
            val layout = RelativeLayout(context)
            createTitleView()
            val titleLayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            titleLayoutParams.leftMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_LEFT)
            titleLayoutParams.topMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_TOP)
            titleLayoutParams.bottomMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_BOTTOM)
            titleLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL)
            titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
            titleView?.layoutParams = titleLayoutParams
            titleView?.id = 9438498
            layout.addView(titleView)

            //Generated the DateView
            createDateView()
            val editTextLayoutParams = if (isEvenly)                                                                                              {
                RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dpToPx(evenlyWith))
            } else {
                RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            }
            editTextLayoutParams.topMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_TOP)
            editTextLayoutParams.bottomMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_BOTTOM)
            editTextLayoutParams.rightMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_RIGHT)
            editTextLayoutParams.addRule(RelativeLayout.RIGHT_OF, titleView?.id!!)
            editTextLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL)

//            editTextLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
            dateView?.layoutParams = editTextLayoutParams
            dateView?.hint = defaultDate
            dateView?.text=value
            dateView?.setTextSize(TypedValue.COMPLEX_UNIT_DIP, HINT_DEFAULT.toFloat())
            dateView?.gravity = Gravity.END+Gravity.CENTER_VERTICAL
            layout.addView(dateView)

            //Generated the Title

            layout
        }
        addOnViewCreatedListener<TextShowLeftRow> {
            dateView?.setOnClickListener {
                onValueClicked()
            }
        }
    }


        //    设置一个下拉的列表选择项




    /**
     * Use this lambda to change the visual aspect of the TitleView
     */
    protected fun createDateView(): TextView {
        dateView = AppCompatTextView(context)
        customizeDateView.invoke(this, this.dateView as TextView)
        return dateView as TextView
    }

    override fun onValueClicked() {
        val resultHandler = ResultHandler<String>()
        resultHandler.registerListener {
            value = it
        }
    }

    override fun value(): String? {
        return value
    }
}